﻿using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;
namespace U.Enterprise.Api.Applications.Scalesetting
{
    public class DeductionsHtmlHandler : IRequestHandler<DeductionsHtmlCommond, PageDTO<Deductions>>
    {
        private readonly IBateRepository<Deductions> _Deductions;

        public DeductionsHtmlHandler(IBateRepository<Deductions> deductions)
        {
            _Deductions = deductions;
        }

        public async Task<PageDTO<Deductions>> Handle(DeductionsHtmlCommond request, CancellationToken cancellationToken)
        {
           
            var res=new PageDTO<Deductions>() { pageCount=0,totalCount=0};


            var list = _Deductions.GetAll().ToList();

           if(request.DeductionName!=null)
            {
                list = list.Where(x => x.DuctionName.Contains(request.DeductionName)).ToList();
            }

           if(request.DeductionType>0)
            {
                list = list.Where(x => x.Stact == request.DeductionType).ToList();
            }



           res.totalCount = list.Count;
           res.pageCount =(int)Math.Ceiling(list.Count*1.0/ list.Count);

            list = list.OrderBy(x => x.Ductionid).Skip(request.pageinedex-1).Take(request.pagesize).ToList();

            res.Lists = list;


            return res;


        }
    }
}
